<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'purchaseplan.add'" ></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'purchaseplan.delete'" ></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.search_keyword"  class="input-with-search" style="width: 300px" @keyup.enter="upsearch">
                        <template #prepend>
                            <el-select v-model="search.search_field" filterable clearable placeholder="请选择" style="width: 110px">
                                <el-option label="订单编号" value="plan_code"/>
                                <el-option label="采购编号" value="purchase_code"/>
                                <el-option label="供应商" value="apply_name"/>
                                <el-option label="地区" value="area_name"/>
                                <el-option label="品名" value="goods_name"/>
                            </el-select>
                        </template>
                    </el-input>
                    <!-- <el-select v-model="search.apply_id" placeholder="供应商" filterable clearable>
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
                    </el-select> -->
                    <el-select v-model="search.plan_type" placeholder="订单类型" filterable>
                        <el-option label="白坯" :value="1"></el-option>
                        <el-option label="半成品" :value="2"></el-option>
                        <el-option label="成品" :value="3"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <!-- <el-input v-model="search.purchase_code" placeholder="关联" clearable @keyup.enter="upsearch"></el-input> -->
                    <el-select v-model="search.is_complate" placeholder="请选择状态" filterable clearable>
                        <el-option label="未完" :value="0"></el-option>
                        <el-option label="完成" :value="1"></el-option>
                        <el-option label="全部" :value="10"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" :params="search" tableName="planTable" :column="planColumn" :cell-style="cellStyle">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <template #expand="props">
                    <scTable :data="props.row.purchase_list" row-key="id" hidePagination hideDo class="prop_table">
                        <el-table-column width="120" />
                        <el-table-column type="selection" width="50" align="center"></el-table-column>
                        <el-table-column label="采购编号" prop="purchase_code" width="250">
                            <template #header>
                                <div class="flex">
                                    <div>采购编号</div>
                                    <el-checkbox v-model="props.is_purchase_all" label="展示全部" style="margin-left: 50px" @change="purchase_all_change(props.row,props.is_purchase_all)" />
                                </div>
                            </template>
                            <template #default="{row}">
                                <div class="flex" style="flex-direction: column;align-items: flex-start">
                                    <div class="flex">
                                        <el-link :underline="false" type="primary" @click="purchase_show(row)">{{ row.purchase_code }}</el-link>
                                        <el-icon class="icon" :size="20" @click="common.copy(row.purchase_code)"><el-icon-Edit /></el-icon>
                                        <el-icon class="icon" :size="20" color="#29f" @click="purchase_bell(row)" v-auth="'purchase.message'"><el-icon-BellFilled /></el-icon>
                                    </div>
                                    <div class="margin">{{row.purchase_date}}</div>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template #default="{row}">
                                <div>交货： <span style="color: #29f">-{{row.total_num}} </span></div>
                                <div class="margin">备注：<el-icon v-auth="'purchase.remark'" class="icon" color="#1e9fff" :size="18" @click="table_remark(row)"><el-icon-EditPen /></el-icon> <span>{{row.remark}}</span></div>
                            </template>
                        </el-table-column>
                        <el-table-column label="入库进度" prop="state" width="130" align="center">
                            <template #default="{row}">
                                <div class="progress" @click="purchase_show(row)">
                                    <div style="margin-bottom: 5px">{{row.deliver_num}}/{{row.total_num}}</div>
                                    <el-progress style="width: 100%" :percentage="((row.deliver_num / row.total_num) * 100)" :show-text="false"></el-progress>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="审核" prop="status" width="130" align="center">
                            <template #default="{row}">
                                <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'purchase.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="签名" prop="upload" width="130" align="center">
                            <template #default="{row}">
                                <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'purchase.upload'">查看</el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="业务员" prop="purchasestaff" width="130" align="center">
                            <template #default="{row}">
                                <div class="flex">
                                    <div>{{row.purchasestaff && row.purchasestaff.name}}</div>
                                    <el-icon class="icon" :size="16" @click="purchase_staff(row)"><el-icon-UserFilled /></el-icon>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" width="320">
                            <template #default="scope">
                                <div class="buttonFlex">
                                    <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="purchase_complate(scope.row)">
                                        <template #reference>
                                            <el-button type="default" size="small" v-auth="'purchase.complate'">{{scope.row.is_complate == 1 ? '完成' : '未完'}}</el-button>
                                        </template>
                                    </el-popconfirm>
                                    <el-popconfirm :title="scope.row.freeze == 0 ? '确定冻结吗？' : '确定撤销冻结吗？'" @confirm="purchase_freeze(scope.row, scope.$index)">
                                        <template #reference>
                                            <el-button type="default" size="small" v-auth="'purchase.freeze'">{{scope.row.freeze == 0 ? '冻结' : '撤销冻结'}}</el-button>
                                        </template>
                                    </el-popconfirm>
                                    <el-button size="small" @click="purchase_print(scope.row)" v-auth="'purchase.print'">打印</el-button>
                                    <el-button type="primary" size="small" @click="purchase_edit(scope.row)" v-auth="'purchase.edit'" v-if="scope.row.status != 1">编辑</el-button>
                                    <el-popconfirm title="确定删除吗？" @confirm="purchase_del(scope.row, scope.$index)">
                                        <template #reference>
                                            <el-button type="danger" size="small" v-auth="'purchase.delete'" v-if="scope.row.status != 1">删除</el-button>
                                        </template>
                                    </el-popconfirm>
                                </div>
                                <div class="buttonFlex">
                                    <el-button size="small" type="default" @click="purchase_settle(scope.row)" v-auth="'purchase.settle'">结账单</el-button>
                                    <el-button size="small" type="default" @click="purchase_copy(scope.row)" v-auth="'purchase.copy'">复制</el-button>
                                    <el-button size="small" type="default" @click="purchase_inhouse(scope.row)" v-auth="'purchasehouse.add'" v-if="scope.row.status == 1">入库</el-button>
                                    <el-button size="small" type="default" @click="purchase_inrecord(scope.row)" v-auth="'purchasehouse.inrecord'">入库记录</el-button>
                                </div>
                            </template>
                        </el-table-column>
                    </scTable>
                </template>
                <template #purchase_code="{row}">
                    <div class="flex start">
                        <el-link :underline="false" type="primary" @click="table_show(row)">{{ row.purchase_code }}</el-link>
                        <el-icon class="icon" :size="20" @click="common.copy(row.purchase_code)"><el-icon-Edit /></el-icon>
                    </div>
                    <div class="margin">{{row.date}}</div>
                </template>
                <template #apply_name="{row}">
                    <div style="margin: 5px 0; cursor: pointer;" @click="cellSearch(row.apply?.apply_name,'apply_name')">{{row.apply?.apply_name}}</div>
                    <div class="margin">{{row.apply?.linkman}}</div>
                </template>
                <template #goods="{row}">
                    <div>产品： <span style="color: #29f; cursor: pointer;" @click="cellSearch(row.goods?.name,'goods_name')">{{row.goods?.name}} </span>
                        <el-tag style="margin-left: 5px;" size="small" :type="row.plan_type == 1 ? 'info' : row.plan_type == 2 ? 'success' : 'danger'">{{row.plan_type == 1 ? '白坯' : row.plan_type == 2 ? '半成品' : '成品'}}</el-tag>
                    </div>
                    <div class="margin">品质： <span style="color: #29f">{{`${row.price}元/米-${row.weight}克/米-${row.plan_num}匹`}}</span></div>
                    <div>备注： <span>{{row.remark}}</span></div>
                </template>
                <template #progress="{row}">
                    <div class="progress">
                        <div style="margin-bottom: 5px">{{row.purchase_number}} / {{row.plan_num}}</div>
                        <el-progress style="width: 100%" :percentage="Math.round((row.purchase_number / row.plan_num) * 100)" :show-text="false" />
                    </div>
                </template>
                <template #operation="scope">
                    <div class="buttonFlex">
                        <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'purchaseplan.edit'" v-if="scope.row.canno_delete == 0">编辑</el-button>
                        <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                            <template #reference>
                                <el-button type="danger" size="small" v-auth="'purchaseplan.delete'" v-if="scope.row.canno_delete == 0">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                    <div class="buttonFlex">
                        <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="plan_complate(scope.row)">
                            <template #reference>
                                <el-button type="default" size="small" v-auth="'purchaseplan.complate'">{{scope.row.is_complate == 1 ? '完成' : '未完'}}</el-button>
                            </template>
                        </el-popconfirm>
                        <el-button type="default" size="small" @click="purchase_add(scope.row)" v-auth="'purchase.add'">采购</el-button>
                    </div>
                </template>

                <!-- <el-table-column type="index" width="50" align="center"></el-table-column> -->
                <!-- <el-table-column type="expand">
                    <template #default="props">
                        <el-checkbox v-model="props.is_purchase_all" label="展示全部" style="margin-left: 50px" @change="purchase_all_change(props.row,props.is_purchase_all)" />
                        <scTable :data="props.row.purchase_list" row-key="id" hidePagination hideDo>
                            <el-table-column width="150" />
                            <el-table-column label="采购编号" prop="purchase_code" width="250">
                                <template #default="{row}">
                                    <div class="flex" style="flex-direction: column;align-items: flex-start">
                                        <div class="flex">
                                            <el-link type="primary" @click="purchase_show(row)">{{ row.purchase_code }}</el-link>
                                            <el-icon class="icon" :size="20" @click="common.copy(row.purchase_code)"><el-icon-Edit /></el-icon>
                                            <el-icon class="icon" :size="20" color="#29f" @click="purchase_bell(row)" v-auth="'purchase.message'"><el-icon-BellFilled /></el-icon>
                                        </div>
                                        <div class="margin">{{row.purchase_date}}</div>
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template #default="{row}">
                                    <div>交货： <span style="color: #29f">-{{row.total_num}} </span></div>
                                    <div class="margin">备注：<el-icon v-auth="'purchase.remark'" class="icon" color="#1e9fff" :size="18" @click="table_remark(row)"><el-icon-EditPen /></el-icon> <span>{{row.remark}}</span></div>
                                </template>
                            </el-table-column>
                            <el-table-column label="入库进度" prop="state" width="130" align="center">
                                <template #default="{row}">
                                    <div class="progress" @click="purchase_show(row)">
                                        <div style="margin-bottom: 5px">{{row.deliver_num}}/{{row.total_num}}</div>
                                        <el-progress style="width: 100%" :percentage="((row.deliver_num / row.total_num) * 100)" :show-text="false"></el-progress>
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column label="审核" prop="status" width="130" align="center">
                                <template #default="{row}">
                                    <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'purchase.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                                </template>
                            </el-table-column>
                            <el-table-column label="签名" prop="upload" width="130" align="center">
                                <template #default="{row}">
                                    <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'purchase.upload'">查看</el-button>
                                </template>
                            </el-table-column>
                            <el-table-column label="业务员" prop="purchasestaff" width="130" align="center">
                                <template #default="{row}">
                                    <div class="flex">
                                        <div>{{row.purchasestaff && row.purchasestaff.name}}</div>
                                        <el-icon class="icon" :size="16" @click="purchase_staff(row)"><el-icon-UserFilled /></el-icon>
                                    </div>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" align="center" width="300">
                                <template #default="scope">
                                    <div class="buttonFlex">
                                        <el-popconfirm :title="scope.row.freeze == 0 ? '确定冻结吗？' : '确定撤销冻结吗？'" @confirm="purchase_freeze(scope.row, scope.$index)">
                                            <template #reference>
                                                <el-button size="small" v-auth="'purchase.freeze'">{{scope.row.freeze == 0 ? '冻结' : '撤销冻结'}}</el-button>
                                            </template>
                                        </el-popconfirm>
                                        <el-button size="small" @click="purchase_print(scope.row)" v-auth="'purchase.print'">打印</el-button>
                                        <el-button type="primary" size="small" @click="purchase_edit(scope.row)" v-auth="'purchase.edit'" v-if="scope.row.status != 1">编辑</el-button>
                                        <el-popconfirm title="确定删除吗？" @confirm="purchase_del(scope.row, scope.$index)">
                                            <template #reference>
                                                <el-button type="danger" size="small" v-auth="'purchase.delete'" v-if="scope.row.status != 1">删除</el-button>
                                            </template>
                                        </el-popconfirm>
                                    </div>
                                    <div class="buttonFlex">
                                        <el-button size="small" @click="purchase_settle(scope.row)" v-auth="'purchase.copy'">结账单</el-button>
                                        <el-button size="small" @click="purchase_copy(scope.row)" v-auth="'purchase.copy'">复制</el-button>
                                        <el-button size="small" @click="purchase_inhouse(scope.row)" v-auth="'purchasehouse.add'" v-if="scope.row.status == 1">入库</el-button>
                                        <el-button size="small" @click="purchase_inrecord(scope.row)" v-auth="'purchasehouse.inrecord'">入库记录</el-button>
                                    </div>
                                </template>
                            </el-table-column>
                        </scTable>
                    </template>
                </el-table-column>
                <el-table-column label="订单编号" prop="purchase_code" width="200">
                    <template #default="{row}">
                        <div class="flex start">
                            <el-link type="primary" @click="table_show(row)">{{ row.purchase_code }}</el-link>
                            <el-icon class="icon" :size="20" @click="common.copy(row.purchase_code)"><el-icon-Edit /></el-icon>
                        </div>
                        <div class="margin">{{row.date}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="供应商" prop="apply.apply_name" width="120">
                    <template #default="{row}">
                        <div style="margin: 5px 0">{{row.apply.apply_name}}</div>
                        <div class="margin">{{row.apply.linkman}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="产品信息" prop="date" width="650">
                    <template #default="{row}">
                        <div>产品： <span style="color: #29f">{{row.goods.name}} </span></div>
                        <div class="margin">品质： <span style="color: #29f">{{`${row.price}元/米-${row.weight}克/米-${row.plan_num}匹`}}</span></div>
                        <div>备注： <span>{{row.remark}}</span></div>
                    </template>
                </el-table-column>
                <el-table-column label="采购进度" align="center" width="150">
                    <template #default="{row}">
                        <div class="progress">
                            <div style="margin-bottom: 5px">{{row.purchase_number}} / {{row.plan_num}}</div>
                            <el-progress style="width: 100%" :percentage="Math.round((row.purchase_number / row.plan_num) * 100)" :show-text="false" />
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="待审核" prop="pend_check" align="center"></el-table-column>
                <el-table-column label="业务员" prop="create_staff_name" align="center"></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="200">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'purchaseplan.edit'" v-if="scope.row.canno_delete == 0">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'purchaseplan.delete'" v-if="scope.row.canno_delete == 0">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                        <div class="buttonFlex">
                            <el-popconfirm :title="`确定${scope.row.is_complate == 1 ? '未完' : '完成'}吗？`" @confirm="plan_complate(scope.row)">
                                <template #reference>
                                    <el-button size="small" v-auth="'purchaseplan.complate'">{{scope.row.is_complate == 1 ? '完成' : '未完'}}</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button size="small" @click="purchase_add(scope.row)" v-auth="'purchase.add'">创建采购</el-button>
                        </div>
                    </template>
                </el-table-column> -->
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <purchase-dialog v-if="dialog.purchase" ref="purchaseDialog" @success="handleSuccess" @closed="dialog.purchase = false"></purchase-dialog><!-- 采购 -->
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
    <look-list v-if="dialog.look" ref="lookDialog" @success="handleSuccess" @closed="dialog.look = false" :query="{purchase_id: purchase_id}"></look-list>
    <arrival-dialog v-if="dialog.arrival" ref="arrivalDialog" @success="handleSuccess" @closed="dialog.arrival = false"></arrival-dialog><!-- 入库 -->
    <notice-save v-if="dialog.notice" ref="noticeDialog" @success="handleSuccess" @closed="dialog.notice = false"></notice-save>

    <!-- <arrival-list v-if="dialog.arrivalrecord" ref="arrivalrecordDialog" @success="handleSuccess" @closed="dialog.arrivalrecord = false"></arrival-list> -->
    <select-house v-if="dialog.house" ref="houseDialog" @success="houseBack" @closed="dialog.house = false" :query="{purchase_id: purchase_id}"></select-house><!-- 入库记录 -->

    <settle-save v-if="dialog.settle" ref="settleDialog" @success="handleSuccess" @closed="dialog.settle = false"></settle-save>

    <remark-save v-if="dialog.remark" ref="remarkDialog" @success="handleSuccess" @closed="dialog.remark = false"></remark-save>

    <el-dialog title="打印" width="500" v-model="dialog.print_select" align-center>
        <el-form label-position="top">
            <el-form-item label="打印关联单">
                <el-select v-model="is_correlation">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="打印价格"> 
                <el-select v-model="is_price">
                    <el-option label="是" :value="1"></el-option>
                    <el-option label="否" :value="0"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button type="primary" @click="open_print">打 印</el-button>
        </template>
    </el-dialog>
    <pruchase-print v-if="dialog.print" ref="printDialog" @success="handleSuccess" @closed="dialog.print = false"></pruchase-print>
</template>

<script>
import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
import saveDialog from './save'
import purchaseDialog from '../purchase/save'
import LookList from '../purchase/lookList.vue'
import arrivalDialog from '../arrival/save'

// import ArrivalList from '../arrival/arrivalList.vue'
import SelectHouse from '../../selectHouse.vue';

import NoticeSave from '../../noticeSave.vue'
import RemarkSave from '../../remarkSave.vue'
import pruchasePrint from '../purchase/pruchasePrint.vue'
import SettleSave from '../purchase/settleSave.vue'

export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, purchaseDialog, uploadDialog, LookList, arrivalDialog, SelectHouse, NoticeSave, RemarkSave, pruchasePrint, SettleSave
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                purchase: false,
                upload: false,
                look: false,
                arrival: false,
                house: false,
                notice: false,
                remark: false,
                print_select: false,
                print: false,
                settle: false,
            },
            apiObj: this.$API.business.purchase.plan.list,
            selection: [],
            search: {
                search_keyword: null,
                search_field: null,
                date: null,
                status: null,
                is_complate: 0,
                purchase_all: [],
            },
            applyList: [],
            purchase_id: "",
            print_id: "",
            is_correlation: 1,
            is_price: 1,
            planColumn: [
                { label: "", prop: "expand", type: 'expand' },
                { label: "订单编号", prop: "purchase_code", width: "200",align: 'left'},
                // { label: "订单类型", prop: "plan_type", width: "120",align: 'left'},
                { label: "供应商", prop: "apply_name", width: "120",align: 'left'},
                { label: "产品信息", prop: "goods", width: "650",align: 'left'},
                { label: "采购进度", prop: "progress", width: "150",align: 'left'},
                { label: "待审核", prop: "pend_check", },
                { label: "业务员", prop: "create_staff_name", },
                { label: "操作", prop: "operation", width: "230",fixed: "right",align: 'left'},
            ],
        }
    },
    mounted() {
		this.getApply();
    },
    methods: {
        cellSearch(keyword,field){
            this.search.search_keyword = keyword
            this.search.search_field = field
            this.$refs.table.upData(this.search)
        },
        cellStyle(){
            let cellStyle = {backgroundColor:'#fff'}
            return cellStyle
        },
        //添加
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            // this.dialog.save = true
            // this.$nextTick(() => {
            //     this.$refs.saveDialog.open('show').setData(row)
            // })
            this.$refs.table.toggleRowExpansion(row)
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.plan.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.purchase.plan.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'purchase') {
                this.$refs.table.refresh()
            } else if (mode == 'copy') {
                this.$refs.table.refresh()
            }
        },
        refresh() {
            // for (const key in this.search) {
            //     this.search[key] = null;
            // }
            // this.search.is_complate = 0
            // this.search.purchase_date = [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],

            // this.$refs.table.upData(this.search)
            window.location.reload() /* 刷新页面 */
        },
        // 供应商列表
		async getApply() {
			var res = await this.$API.basic.apply.all.get({type: 1});
			this.applyList = res.data
		},
        // 创建采购
        purchase_add(row){
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('add').getData(row)
            })
        },
        // 编辑采购
        purchase_edit(row){
            if(row.freeze == 1){
                this.$message.error("该采购单已冻结，请解冻后再进行编辑！")
                return
            }
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('edit').setData(row)
            })
        },
        // 采购复制
        purchase_copy(row){
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('copy').setData(row,1)
            })
        },
        // 查看采购
        purchase_show(row){
            this.dialog.purchase = true
            this.$nextTick(() => {
                this.$refs.purchaseDialog.open('show').setData(row)
            })
        },
        //删除采购
        async purchase_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.order.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        // 完成状态切换
        async plan_complate(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.plan.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        // 采购审核
        purchase_check(row){
            if(row.freeze == 1){
                this.$message.error("该采购单已冻结，请解冻后再进行审核！")
                return
            }
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('purchase').setData(row,row.checkstaff?.name)
            })
        },
        // 查看签名
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
        // 采购完成
        async purchase_complate(row){
            var reqData = { id: row.id }
            var res = await this.$API.business.purchase.order.complate.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //  采购冻结/反冻结
        async purchase_freeze(row) {
            var reqData = {
                id: row.id,
                freeze: row.freeze == 1 ? 0 : 1
            }
            var res = await this.$API.business.purchase.order.freeze.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        // 查看采购员
        purchase_staff(row){
            this.purchase_id = row.id
            this.dialog.look = true
            this.$nextTick(() => {
                this.$refs.lookDialog.open('edit')
            })
        },
        // 采购入库
        purchase_inhouse(row){
            this.dialog.arrival = true
            this.$nextTick(() => {
                this.$refs.arrivalDialog.open('add').getData(row)
            })
        },
        // 入库记录
        purchase_inrecord(row){
            this.purchase_id = row.id
            this.dialog.house = true
            this.$nextTick(() => {
                this.$refs.houseDialog.open('edit')
            })
        },
        // 采购提醒
        purchase_bell(row){
            let data = {
                id: row.id,
                code: row.purchase_code,
                apply_id: row.apply_id
            }
            this.dialog.notice = true
            this.$nextTick(() => {
                this.$refs.noticeDialog.open('edit').setData(data,'采购单',1)
            })
        },
        
        // 采购单备注
        table_remark(row){
            this.dialog.remark = true
            this.$nextTick(() => {
                this.$refs.remarkDialog.open('purchase').setData(row)
            })
        },

        purchase_all_change(row, is_purchase_all) {
            if (is_purchase_all) {
                if (!this.search.purchase_all.some(item => item.purchase_plan_id === row.id)) {
                    this.search.purchase_all.push({
                        purchase_plan_id: row.id,
                        is_purchase_all: 1
                    });
                }
            } else {
                this.search.purchase_all = this.search.purchase_all.filter(
                    item => item.purchase_plan_id !== row.id
                );
            }
            this.search.is_complate = null
            this.$refs.table.upData(this.search);
        },
        // 打印选择弹窗
        purchase_print(row){
            this.common.copy(row.purchase_code)
            this.print_id = row.id;
            this.dialog.print_select = true;
        },
        // 确定打印
        open_print(){
            let query = {
                id: this.print_id,
                is_correlation: this.is_correlation,
                is_price: this.is_price,
            }
            this.dialog.print_select = false;
            this.dialog.print = true;
            this.$nextTick(() => { 
                this.$refs.printDialog.open('print').setData(query)
            })
            this.is_correlation = 1
            this.is_price = 1
        },

        // 结算
        purchase_settle(row){
            this.purchase_id = row.id
            this.dialog.house = true
            this.$nextTick(() => {
                this.$refs.houseDialog.open('add').setData(1)
            })
            // this.dialog.settle = true;
            // this.$nextTick(() => { 
            //     this.$refs.settleDialog.open('edit').setData(row)
            // })
        },
        houseBack(data){
            let array = []
            data.map(item=>{
                array.push(item.id)
            })
            let row = {
                id: this.purchase_id,
                purchase_ware_ids: array
            }
            this.dialog.settle = true;
            this.$nextTick(() => { 
                this.$refs.settleDialog.open('edit').setData(row)
            })
        },
    }
}
</script>

<style lang="scss">
.el-tag{
    min-height: 20px !important;
    height: 20px !important;
    line-height: 20px !important;
}
.margin{
    margin: 5px 0;
}
.flex {
    display: flex;
    align-items: center;
    justify-content: center;
    .icon{
        cursor: pointer;
        margin-left: 5px;
    }
}
.progress{
    width: 100%;
    display: flex;
    flex-direction: column;
}
</style>
